<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kontak_Model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_detil_kontak($notelp) {
        $sql = "select * from kontak_member where id_member_owner=? and notelp_kontak=?";
        $query = $this->db->query($sql, array($this->session->userdata("id_member"), $notelp));
        $result = $query->result();
        if ($query->num_rows() > 0) {
            return $result;
        } else {
            return null;
        }
    }

    function update_kontak($id_kontak, $nama_kontak, $notelp_kontak, $group, $keterangan) {
        $sql = "update kontak_member set nama_kontak=?, notelp_kontak=?, group_kontak=?, keterangan=? where id_kontak=? and id_member_owner=?";
        $this->db->query($sql, array($nama_kontak, $notelp_kontak, $group, $keterangan, $id_kontak, $this->session->userdata("id_member")));
        return json_encode(array("err_code" => "00", "msg" => "Data Kontak Berhasil Disimpan"));
    }

    function insert_kontak($nama_kontak, $notelp_kontak, $group, $keterangan) {
        $sql = "select nama_kontak from kontak_member where notelp_kontak=? and id_member_owner=?";
        $query = $this->db->query($sql, array($notelp_kontak, $this->session->userdata("id_member")));        
        if ($query->num_rows() > 0) {
            return json_encode(array("err_code" => "00", "msg" => "Data Kontak Sudah Ada"));
        } else {
            $sql = "insert into kontak_member(id_member_owner,nama_kontak,notelp_kontak,group_kontak,keterangan) values (?,?,?,?,?)";
            $this->db->query($sql, array($this->session->userdata("id_member"),$nama_kontak, $notelp_kontak, $group, $keterangan));
            return json_encode(array("err_code" => "00", "msg" => "Data berhasil disimpan"));
        }
    }

}
